/*************************************************************************
> File Name: ForEach.cpp
> Author: icebear
> Mail: 3322366195@qq.com
> Created Time: 2023-08-02 09:56:11
> Description: 一次遍历
************************************************************************/

#include <iostream>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
using namespace std;
class Solution
{
public:
    int myAtoi(string s)
    {
        while (s != "" && s[0] == ' ') // 去除空格
        {
            s.erase(0, 1);
        }
        if (s == "") // 空字符
            return 0;
        bool negative = s[0] == '-' ? true : false; // 正负
        if (negative || s[0] == '+')                // 正数去掉符号
        {
            s.erase(0, 1);
        }
        if (s == "")
        {
            return 0;
        }
        long long s_long = 0;
        for (auto it = s.begin(); it != s.end() && (*it) - '0' >= 0 && (*it) - '0' <= 9; ++it)
        { // 转化数字
            s_long *= 10;
            s_long += (*(it) - '0');
            if (s_long > (long long)INT_MAX || s_long < (long long)INT_MIN)
                return negative ? INT_MIN : INT_MAX;
        }
        return negative ? -s_long : s_long; // 判断越界
    }
};
int main(int argc, char const *argv[])
{
    Solution solution;
    string x = "1534236469";
    int result = solution.myAtoi(x);
    cout << result << endl;

    x = "-2147483412";
    result = solution.myAtoi(x);
    cout << result << endl;
    return 0;
}
